<template>
  <div>
    <!-- 面包削区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加角色 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addRolesDialog = true">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表 -->
      <el-table
        :stripe="true"
        :border="true"
        :data="RoleList"
        style="width: 100%">
        <!-- 展开行 -->
        <el-table-column width="48" type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom',i1==0?'bdtop':'','gocenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
              <el-col :span="5">
                <el-tag closable @close="closeTagById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2,i2) in item1.children" :key="item2.id" :class="[i2==0?'':'bdtop','gocenter']">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="closeTagById(scope.row,item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18" >
                    <el-tag type="warning" v-for="(item3,i3) in item2.children" :key="item3.id" closable @close="closeTagById(scope.row,item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" width="48" type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" ></el-table-column>
        <el-table-column  label="操作" >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRole(scope.row)">删除</el-button>
            <el-button type="warning" icon="el-icon-s-tools" size="mini" @click="allocationClick(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限弹出框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="allocationDialog"
      width="50%">
      <!-- 树形控件 -->
      <el-tree :data="treeList" :props="treeProps" 
        show-checkbox node-key="id" :default-expand-all="true" :default-checked-keys="defaultKeys" ref="treeRef">
      </el-tree>
      <span slot="footer">
        <el-button @click="allocationDialog = false">取 消</el-button>
        <el-button type="primary" @click="authorityDistribution">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加角色对话框 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addRolesDialog"
      width="50%">
      <el-form :model="addRolesForm" :rules="addRolesRules" ref="addRolesRef" label-width="100px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addRolesDialog = false">取 消</el-button>
        <el-button type="primary" @click="addRolesSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {DeleteOneRoleData,RoleListData,DeleteRoleData,AuthorityTreeManagementData,authorityDistributionData,addRolesData} from '@/request/api'
export default {
  name:'Roles',
  data(){
    return {
      //添加角色表单
      addRolesForm:{
        roleName:'',
        roleDesc:''
      },
      addRolesRules:{
        roleName: [
            { required: true, message: '请输入角色名称', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      //角色id
      RoleId:'',
      //tree树状权限列表数据
      treeList:[],
      //默认勾选的权限
      defaultKeys:[],
      // 角色数据列表
      RoleList:[],
      // 分配权限弹出框控制
      allocationDialog:false,
      //添加角色弹出框控制
      addRolesDialog:false,
      treeProps:{
        children: 'children',
        label: 'authName'
      }
    }
  },
  created(){
    this.getRoleListData()
  },
  methods:{
    //添加角色
    addRolesSure(){
      this.$refs.addRolesRef.validate(valid=>{
        if(valid){
          addRolesData(this.addRolesForm)
          .then(res=>{
            // console.log(res);
            if(res.meta.status == 201){
              this.$message.success(res.meta.msg)
              this.getRoleListData()
            }
          })
        }
      })
      this.addRolesDialog = false
    },
    //删除角色
    deleteRole(item){
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((data) => {
        if(data=='confirm'){
          DeleteOneRoleData({
            id:item.id
          }).then(res=>{
            if(res.meta.status==200){
              this.getRoleListData()
              this.$message.success(res.meta.msg)
              
            }
          })
        }
      }).catch((data_) => {
        if(data_=='cancel'){
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
          return  
        }   
      });
    },
    //给角色授权
    authorityDistribution(){
      let keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      let idKeys = keys.join(',')
      authorityDistributionData({
        roleId:this.RoleId,
        rids:idKeys
      }).then(res=>{
        if(res.meta.status == 200){
          this.$message.success(res.meta.msg)
          this.getRoleListData()
        }
      })
      this.allocationDialog = false
    },
    //通过递归来获取默认节点id
    getNodeKeys(node,arr){
      if(!node.children){
        return arr.push(node.id)
      }
      node.children.forEach(item=>{
        this.getNodeKeys(item,arr)
      })
    },
    //分配权限弹出框
    allocationClick(item){
      // 角色id
      this.RoleId = item.id
      AuthorityTreeManagementData({
        type:'tree'
      }).then(res=>{
        if(res.meta.status==200){
          this.treeList = res.data
          // console.log(this.treeList);
        }
      })
      this.defaultKeys=[]
      this.getNodeKeys(item,this.defaultKeys)

      this.allocationDialog=true
    },
    //获取角色列表数据
    getRoleListData(){
      RoleListData().then(res=>{
        if(res.meta.status==200){
          this.RoleList = res.data
        }
      })
    },
    // 删除角色指定权限
    closeTagById(data1,data2){
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((data) => {
        if(data=='confirm'){
          DeleteRoleData({
            // 角色 ID
            roleId:data1.id,
            // 权限 ID
            rightId:data2
          }).then(res=>{
            if(res.meta.status ==200){
              this.$message.success(res.meta.msg)
              // this.getRoleListData()
              data1.children = res.data
            }
          })
        }
      }).catch((data_) => {
        if(data_=='cancel'){
          this.$message({ 
            type: 'info',
            message: '已取消删除'
          });
          return  
        }   
      });
    }
  }
}
</script>

<style scoped lang="less">
  .el-table{
    margin-top: 15px;
  }
  .el-tag{
      margin: 7px;
  }
  .bdtop{
    border-top: 1px solid #eee;
  }
  .bdbottom{
    border-bottom: 1px solid #eee;
  }
  .gocenter{
    display: flex;
    align-items: center;
  }
</style>